package com.imooc.service.impl;

import com.imooc.common.utils.bean.BeanUtils;
import com.imooc.common.utils.common.collect.ListUtils;
import com.imooc.enums.YesOrNo;
import com.imooc.mapper.UserAddressMapper;
import com.imooc.pojo.BO.AddressBO;
import com.imooc.pojo.UserAddress;
import com.imooc.service.AddressService;
import org.n3r.idworker.Sid;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lihongyu
 * @CreateTime 2021-10-18 15:14
 * @Description 收货地址业务逻辑实现类
 */
@Service
public class AddressServiceImpl implements AddressService {

    @Resource
    private UserAddressMapper userAddressMapper;

    @Resource
    private Sid sid;

    /**
     * 根据用户id与地址id查询用户地址信息
     *
     * @param userId    用户id
     * @param addressId 地址id
     * @return 用户地址信息
     */
    @Override
    public UserAddress queryUserAddress(String userId, String addressId) {
        UserAddress userAddress = new UserAddress();
        userAddress.setId(addressId);
        userAddress.setUserId(userId);
        return userAddressMapper.selectOne(userAddress);
    }

    /**
     * 设置用户收货地址为默认收货地址
     *
     * @param userId    用户ID
     * @param addressId 地址Id
     * @return boolean 成功/失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public boolean setDefaultUserAddress(String userId, String addressId) {
        //判断用户之前是否存在其他默认收货地址
        List<UserAddress> userAddressList = queryAllAddressList(userId);
        if(ListUtils.isNotEmpty(userAddressList)){
            List<UserAddress> addressList = userAddressList.stream().filter(item -> item.getIsDefault() == 1).collect(Collectors.toList());
            addressList.forEach(address -> {
                address.setIsDefault(YesOrNo.NO.type);
                userAddressMapper.updateByPrimaryKeySelective(address);
            });
        }
        UserAddress userAddress = new UserAddress();
        userAddress.setId(addressId);
        userAddress.setUserId(userId);
        userAddress.setIsDefault(YesOrNo.YES.type);
        return userAddressMapper.updateByPrimaryKeySelective(userAddress) > 0;
    }

    /**
     * 删除用户收货地址
     *
     * @param userId    用户ID
     * @param addressId 地址Id
     * @return boolean 成功/失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public boolean deleteUserAddress(String userId, String addressId) {
        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(userId);
        userAddress.setId(addressId);
        return userAddressMapper.delete(userAddress) > 0;
    }

    /**
     * 修改收货地址
     *
     * @param addressBO 修改收货地址参数封装实体
     * @return boolean 成功/失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public boolean updateUserAddress(AddressBO addressBO) {
        UserAddress userAddress = new UserAddress();
        BeanUtils.copyBeanProp(userAddress,addressBO);
        userAddress.setId(addressBO.getAddressId());
        userAddress.setUpdatedTime(new Date());
        return userAddressMapper.updateByPrimaryKeySelective(userAddress) > 0;
    }

    /**
     * 新增收货地址
     *
     * @param addressBO 新增收货地址参数封装实体
     * @return boolean 成功/失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public boolean addUserAddress(AddressBO addressBO) {
        //判断是否存在历史地址,如果没有将本次地址设置为默认地址
        String userId = addressBO.getUserId();
        List<UserAddress> userAddressList = queryAllAddressList(userId);
        int isDefault = YesOrNo.NO.type;
        if(ListUtils.isEmpty(userAddressList)){
            isDefault = YesOrNo.YES.type;
        }
        //保存收货地址
        UserAddress userAddress = new UserAddress();
        BeanUtils.copyBeanProp(userAddress,addressBO);
        userAddress.setId(sid.nextShort());
        userAddress.setIsDefault(isDefault);
        userAddress.setCreatedTime(new Date());
        userAddress.setUpdatedTime(new Date());
        return userAddressMapper.insert(userAddress) > 0;
    }

    /**
     * 查询用户所有收货地址
     *
     * @param userId 用户Id
     * @return 用户所有收货地址
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.SUPPORTS)
    public List<UserAddress> queryAllAddressList(String userId) {
        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(userId);
        return userAddressMapper.select(userAddress);
    }
}
